<?php

/**
 * Created by PhpStorm.
 * User: evan
 * Date: 16/9/1
 * Time: 下午6:37
 */
class Base extends MY_Admin_Controller
{
    public function index()
    {
    	$page = intval($this->input->get_post('page'));
    	$page = $page ? $page : 1;


    	$uid        = intval($this->input->get_post('uid'));
    	$nickname   = trim($this->input->get_post('nickname'));
        $win_uid    = intval($this->input->get_post('win_uid'));
        $win_nickname = trim($this->input->get_post('win_nickname'));
    	$start_time = trim($this->input->get_post('start_time'));
    	$end_time   = trim($this->input->get_post('end_time'));
        $status     = $this->input->get_post('status');


    	$where = array();
        ($status != NULL && $status >= 0) && $where['status'] = $status;
        $uid && $where['uid'] = $uid;
        $nickname && $where[' uname like "%'.$nickname.'%"'] = null;
        $win_uid && $where['win_uid'] = $win_uid;
        $win_nickname && $where[' win_uname like "%'.$win_nickname.'%"'] = null;
        $start_time && $where['create_time>'] = $start_time;
        $end_time && $where['create_time<'] = $end_time;
    	

    	$this->load->model('auction/Model_auction','aution');
    	$total_rows = $this->aution->count($where);
    	
    	//分页
    	$this->load->library('pagination');
    	 
    	$config['total_rows'] = $total_rows;
    	$config['per_page']   = 20;
    	$config['base_url']   = '/auction/Base/index';
    	 
    	$this->pagination->initialize($config);
    	$show_page = $this->pagination->create_links();
    	$offset = ($page-1) * $config['per_page'];


    	$autionInfo = $this->aution->lists($offset,  $config['per_page'], $where);

        //uid为key name为val
        $this->load->model('auction/Model_auction_cate','auction_cate');
        $listCate = $this->auction_cate->getAllLists();
        $cateInfo = array();
        foreach($listCate as $v)
        {
            $cateInfo[$v['cid']] = $v['name'];
        }

        $data = array
        (
            'uid'          => $uid,
            'nickname'     => $nickname,
            'win_uid'      => $win_uid,
            'win_nickname' => $win_nickname,
            'start_time'   => $start_time,
            'end_time'     => $end_time,
            'status'       => $status,
            'autionInfo'   =>   $autionInfo,
            'cateInfo'     =>   $cateInfo,
        	'show_page'    => $show_page,
        	'total_rows'   => $config['total_rows'],
        );

        $this->load->view('admin/auction/base/index',$data);
    }

    public function create()
    {
        $aid = (int)$this->uri->segment(4);

        //竞拍等级
        $this->load->model('auction/Model_auction', 'auction');
        $auctionInfo = $this->auction->getById($aid);

        //竞拍分类信息
        $this->load->model('auction/Model_auction_cate', 'cate');
        $listCate  = $this->cate->getAllLists();

        //图片信息
        $this->load->model('common/Model_file','file');
        $fileInfo = $this->file->getById($auctionInfo['pic']);

        $data = array
        (
            'auctionInfo' => $auctionInfo,
            'listCate'    => $listCate,
            'fileInfo'    => $fileInfo
        );

        $this->load->view('admin/auction/base/create',$data);
    }

    /*
     * 竞拍资料编辑后保存
     *
     * */
    public function save()
    {
        $aid = (int)$this->input->get_post('aid');

        $data = array(
            'cid'           =>      (int)$this->input->get_post('cid'),
            'uname'         =>      trim($this->input->get_post('uname')),
            'title'         =>      trim($this->input->get_post('title')),
            'play_time'     =>      $this->input->get_post('play_time'),
            'duration'      =>      (int)$this->input->get_post('duration'),
            'time_limit'    =>      (int)$this->input->get_post('time_limit'),
            'start_price'   =>      $this->input->get_post('start_price'),
            'min_fare'      =>      $this->input->get_post('min_fare'),
            'start_time'    =>      $this->input->get_post('start_time'),
            'end_time'      =>      $this->input->get_post('end_time'),
            'summary'       =>      $this->input->get_post('summary'),
            //判断是否选择图片如果选择了图片则获取新突破地址或者判断是新建页面还是修改页面
            'pic'           =>     !empty($_FILES['up_file']['tmp_name'])?$this->uploadFile('up_file'):(isset($aid)?$this->input->get_post('pic'):''),
            'status'        =>      $this->input->get_post('status'),
            //此处直播状态因由直播模块内控制
            'view_num'      =>      (int)$this->input->get_post('view_num'),
            'like_num'      =>      (int)$this->input->get_post('like_num'),
            'focus_num'     =>      (int)$this->input->get_post('focus_num'),
            'identification'=>      $this->input->get_post('identification'),

            //以下字段暂时不能修改
//            'win_uname'     =>      trim($this->input->get_post('win_uname')),
//            'win_amount'    =>      trim($this->input->get_post('win_amount')),
//            'win_time'      =>      trim($this->input->get_post('win_time')),
        	'sort'			=>      (int)$this->input->get_post('sort'),
        );

        //更新竞拍表
        $this->load->model('auction/Model_auction', 'auction');
        $this->auction->save($data, $aid);

        $data_direct = array(
            'cid'            =>      (int)$this->input->get_post('cid'),
            'start_time'     =>      $this->input->get_post('play_time'),
        );
        //更新直播表
        $this->load->model('direct_seeding/Model_direct_seeding', 'direct');
        $directInfo = $this->direct->getOne(array('aid'=>$aid));
        
        if($directInfo)
        {
        	$this->direct->save($data_direct, $directInfo['ds_id']);
        }

        $this->load->helper('url');
        redirect(url('admin').'auction/base/index');
    }


    /**
     * 列表编辑状态保存
     */
    public function save_status()
    {
        $response = array('code' => '1', 'msg' => '修改成功');


        do
        {
            $aid    = (int)$this->input->get_post('aid');
            $status = (int)$this->input->get_post('status');


            $this->load->model('auction/Model_auction', 'a');
            $item = $this->a->getById($aid);
            if (empty($item))
            {
                $response = error(50008);
                break;
            }


            $save_rs = $this->a->save(array('status' => $status), $aid);
            if (!$save_rs)
            {
                $response = error(50058);
                break;
            }


            if (AUCTION_STATUS_NOT_PASSED != $status)
            {
                break;
            }


            //审核不通过时，退竟拍费
            $this->load->model('finance/Model_finance_base_log', 'fbl');
            $this->load->model('auction/Model_auction_log', 'al');
            $auction_highest = $this->al->getOne(array('aid' => $aid, 'is_thaw' => FINANCE_IS_THAW_NO), '*', 'amount desc');
            if (!empty($auction_highest))
            {
                $thaw_rs = $this->fbl->thaw($auction_highest['uid'], $auction_highest['lid'], FINANCE_BUSINESS_AUCTION, 'THAW_AUCTION', '解冻_竞拍');


                //解冻竞拍日志
                $al_thaw_rs = $this->al->thaw($auction_highest['lid']);
            }


            //审核不通过时，退门票
            $this->load->model('direct_seeding/Model_direct_seeding_ticket', 'dst');
            $dst_item = $this->dst->getAllLists(array('aid' => $aid, 'status' => TICKET_STATUS_NOT_USED));
            foreach ($dst_item as $v)
            {
                $thaw_rs = $this->fbl->thaw($v['uid'], $v['tid'], FINANCE_BUSINESS_TICKET, 'THAW_TICKET', '解冻_门票');


                $this->dst->save(array('status' => TICKET_STATUS_REFUND), $v['tid']);
            }
        } while (false);


        $this->json_output($response);
    }


    /**
     * 删除拍卖
     */
    public function delete()
    {
        $response = array('code' => '0', 'msg' => '操作成功');

        do {
            $aid = (int)$this->input->get_post('aid');

            if (!$aid)
            {
                $response = error(50008);
                break;
            }

            $this->load->model('auction/model_auction', 'auction');

            $res = $this->auction->delete(array('aid' => $aid));

            if (!$res)
            {
                $response = error(10080);
                break;
            }
        }while (false);


        $this->json_output($response);
    }


    /*
     * 竞拍出价列表
     * */
    public function log(){
        $aid = $this->uri->segment(4);

        $this->load->model('auction/Model_auction','aution');
        $autionInfo = $this->aution->getAllLists();

        $this->load->model('auction/Model_auction_log','auction_log');

        if($aid)
        {
            $autionlog = $this->auction_log->lists(0,1000,array('aid'=>$aid));
        }
        else
        {
            $autionlog = $this->auction_log->getAllLists();
        }

        $data = array(
            'autionInfo'     =>   $autionInfo,
            'autionlog'      =>   $autionlog,
        );

        $this->load->view('admin/auction/base/log',$data);
    }

    /*
    * 握手列表
    * */
    public function handshake(){
        $aid = $this->uri->segment(4);

        $this->load->model('auction/Model_auction','aution');
        $autionInfo = $this->aution->getAllLists();

        $this->load->model('auction/Model_auction_handshake','handshake');

        if($aid)
        {
            $handshake = $this->handshake->lists(0,1000,array('aid'=>$aid));
        }
        else
        {
            $handshake = $this->handshake->getAllLists();
        }

        $data = array(
            'autionInfo'     =>   $autionInfo,
            'handshake'      =>   $handshake,
        );

        $this->load->view('admin/auction/base/handshake',$data);
    }


    /*
    * 握手列表
    * */
    public function comment(){
        $aid = $this->uri->segment(4);

        $this->load->model('common/Model_comment','comment');

        $comment = $this->comment->lists(0,100000,array('business_id'=>$aid,'by_module'=>MODULE_AUCTION));

        $this->load->model('auction/Model_auction','aution');
        $autionInfo = $this->aution->getByid($aid);

        foreach ($comment as $k=>$v){
            if($v['reply_uid'] && $v['reply_content']){
                $comment[$k]['reply'][0] = array(
                    'reply_uid'     => $v['reply_uid'],
                    'reply_uname'   => $v['reply_uname'],
                    'reply_content' => $v['reply_content'],
                    'reply_ip'      => $v['reply_ip'],
                    'reply_time'    => $v['reply_time'],
                );
                unset($comment[$k]['reply_uid']);
                unset($comment[$k]['reply_uname']);
                unset($comment[$k]['reply_content']);
                unset($comment[$k]['reply_ip']);
                unset($comment[$k]['reply_time']);
            }
        }

//        foreach($comment as $k=>$v){
//
//        }
        $comment = array_values($comment);

        for($i=0;$i<count($comment);$i++){
            for($j=$i+1;$j<count($comment);$j++){
                if($comment[$i]['by_module']==$comment[$j]['by_module'] &&
                    $comment[$i]['content']==$comment[$j]['content']&&
                    $comment[$i]['business_id']==$comment[$j]['business_id']&&  $comment[$i]['uid']==$comment[$j]['uid']
                ){
                    $len = count($comment[$i]['reply']);
                    $comment[$i]['reply'][$len] =  $comment[$j]['reply'][0];

                    unset($comment[$j]);
                }
            }
        }

        $data = array(
            'comment'      =>   $comment,
            'title'        =>   $autionInfo['title']
        );

        $this->load->view('admin/auction/base/comment',$data);
    }

    /*
     *
     * 竞拍分类
     * */
    public function cate_index()
    {
        $this->load->model('auction/model_auction_cate','cate');
        $cateInfo = $this->cate->getAllLists();

        $data = array(
            'cateInfo'  =>   sort_data($cateInfo),
        );

        $this->load->view('admin/auction/base/cate_index',$data);
    }

    public function cate_create()
    {
        $cid = $this->uri->segment(4);

        $this->load->model('auction/model_auction_cate','cate');
        $cateInfo = $this->cate->getById($cid);
        $cateAll = $this->cate->getAllLists();

        if($cid){
            //找出当前下面的分类
            $sonInfo = get_son($cateAll,$cid);
            //取出所有分类下包含的下级分类
            foreach($sonInfo as $k=>$v){
                unset($cateAll[$k]);
            }
        }

        $data = array(
            'cateInfo'     => $cateInfo,
            'cateAll'      => sort_data($cateAll),
        );

        $this->load->view('admin/auction/base/cate_create',$data);
    }

    public function cate_save()
    {
        $cid = (int)$this->input->get_post('cid');
        $this->load->helper('pinyin');

        //图片未存入
        $data = array(
            'name'          =>     trim($this->input->get_post('name')),
            'pid'           =>     (int)$this->input->get_post('pid'),
            'sort'          =>     (int)$this->input->get_post('sort'),
            'path'          =>     pinyin(trim($this->input->get_post('path'))),
            'title'         =>     trim($this->input->get_post('title')),
            'keyword'       =>     trim($this->input->get_post('keyword')),
            'descr'         =>     trim($this->input->get_post('descr')),
        );

        //更新
        $this->load->model('auction/model_auction_cate','cate');
        $this->cate->save($data, $cid);

        $this->load->helper('url');
        redirect(url('admin').'auction/base/cate_index');
    }

    /**
     * 删除分类
     */
    public function cate_delete()
    {
        $response = array('code' => '0', 'msg' => '删除成功');

        do {
            $cid = (int)$this->input->get_post('cid');

            if (!$cid)
            {
                $response = error(50028);
                break;
            }

            $this->load->model('auction/model_auction','auction');
            $item = $this->auction->getOne(array('cid' => $cid));

            if (!empty ($item))
            {
                $response = error(50029);
                break;
            }

            $this->load->model('auction/model_auction_cate', 'auction_cate');

            $item = $this->auction_cate->getOne(array('pid' => $cid));
            if (!empty ($item))
            {
                $response = error(50030);
                break;
            }

            $item = $this->auction_cate->delete(array('cid' => $cid));

            if (empty ($item))
            {
                $response = error(10080);
                break;
            }


        }while (false);

        $this->json_output($response);
    }
}